﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartControls
{
    //Button with an image custom control.
    public class PictureButton : Control
    {
        private Image backgroundImg;
		private Image pressedImg;
		private Image disabledImg;
		private bool pressed = false;
        // Property for the background image to be drawn behind the button text.
        public Image BackgroundImage
        {
            get
            {
                return this.backgroundImg;
            }
            set
            {
                this.backgroundImg = value;
				Invalidate();
            }
        }

        // Property for the background image to be drawn behind the button text when
        // the button is pressed.
		public Image PressedImage
		{
			get
			{
				return this.pressedImg;
			}
			set
			{
				this.pressedImg = value;
				Invalidate();
			}
		}

		public Image DisabledImage
		{
			get
			{
				return this.disabledImg;
			}
			set
			{
				this.disabledImg = value;
				Invalidate();
			}
		}

		// Invalidate form to cause repaint.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.Focus();
            this.pressed = true;
            this.Invalidate();
            base.OnMouseDown(e);
        }
        // When the mouse is released, reset the "pressed" flag
        // and invalidate to redraw the button in the un-pressed state.
        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.pressed = false;
            this.Invalidate();
            base.OnMouseUp(e);
        }

		protected override void OnEnabledChanged(EventArgs e)
		{
			this.Invalidate();
			base.OnEnabledChanged(e);
		}

        bool drawBorder = true;
        public bool DrawBorder
        {
            get { return drawBorder; }
            set { drawBorder = value; }
        }

        // Override the OnPaint method so we can draw the background image and the text.
        protected override void OnPaint(PaintEventArgs e)
        {
            if (DrawBorder)
            {
                if (this.Enabled == false && this.disabledImg != null)
                    e.Graphics.DrawImage(this.disabledImg, new Rectangle(1, 1, this.Width - 2, this.Height - 2), new Rectangle(0, 0, this.backgroundImg.Width, this.backgroundImg.Width), GraphicsUnit.Pixel);
                else if ((this.pressed) && (this.pressedImg != null))
                    e.Graphics.DrawImage(this.pressedImg, new Rectangle(1, 1, this.Width - 2, this.Height - 2), new Rectangle(0, 0, this.pressedImg.Width, this.pressedImg.Width), GraphicsUnit.Pixel);
                else if (this.backgroundImg != null)
                    e.Graphics.DrawImage(this.backgroundImg, new Rectangle(1, 1, this.Width - 2, this.Height - 2), new Rectangle(0, 0, this.backgroundImg.Width, this.backgroundImg.Width), GraphicsUnit.Pixel);

                // Draw the text if there is any.
                if (this.Text.Length > 0)
                {
                    SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

                    // Center the text inside the client area of the PictureButton.
                    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), (this.ClientSize.Width - size.Width) / 2, (this.ClientSize.Height - size.Height) / 2);
                }

                // Draw a border around the outside of the
                // control to  look like Pocket PC buttons.
                e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
            }
            else
            {
                if (this.Enabled == false && this.disabledImg != null)
                    e.Graphics.DrawImage(this.disabledImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, this.backgroundImg.Width, this.backgroundImg.Width), GraphicsUnit.Pixel);
                else if ((this.pressed) && (this.pressedImg != null))
                    e.Graphics.DrawImage(this.pressedImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, this.pressedImg.Width, this.pressedImg.Width), GraphicsUnit.Pixel);
                else if (this.backgroundImg != null)
                    e.Graphics.DrawImage(this.backgroundImg, new Rectangle(0, 0, this.Width, this.Height), new Rectangle(0, 0, this.backgroundImg.Width, this.backgroundImg.Width), GraphicsUnit.Pixel);

                // Draw the text if there is any.
                if (this.Text.Length > 0)
                {
                    SizeF size = e.Graphics.MeasureString(this.Text, this.Font);

                    // Center the text inside the client area of the PictureButton.
                    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), (this.ClientSize.Width - size.Width) / 2, (this.ClientSize.Height - size.Height) / 2);
                }
            }
            base.OnPaint(e);

        }
    }
}